package com.eslserver.action.group;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eslserver.core.aware.GroupAware;
import com.eslserver.dao.DAOException;
import com.eslserver.dao.ESLServerDAOFactory;
import com.eslserver.dao.GroupDAO;
import com.eslserver.to.Group;
import com.eslserver.util.JSONResponseUtil;

@SuppressWarnings("serial")
public class LoadGroupInfo extends HttpServlet implements GroupAware {
	public static final Logger LOG = LoggerFactory
			.getLogger(LoadGroupInfo.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		PrintWriter writer = resp.getWriter();
		try {
			Group group = Group.createGroup();
			String groupID = req.getParameter(PARAM_GROUP_ID);
			if (groupID != null && Integer.valueOf(groupID) != 0) {
				GroupDAO groupDAO = ESLServerDAOFactory.getDAOFactory(
						ESLServerDAOFactory.ESLSERVER_DAOFACTORY).getGroupDAO();
				group = groupDAO.loadGroup(Integer.parseInt(groupID));
			}
			writer.println(JSONResponseUtil.getJSONObjectString(group));
		} catch (DAOException e) {
			LOG.error("error laoding Group Info", e);
		} finally {
			writer.close();
		}
	}

}
